<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/13 0013
 * Time: 上午 8:41
 */

namespace Home\Controller;


use function Sodium\add;
use Think\Controller;
use Think\Page;

class UserInfoController extends Controller
{

    /**
     * 管理员信息
     */
    function admin_Info(){


        //默认查询
        $m = M("ssg_account");
        $count = $m->count();//总条数
        $sum = ceil($count/10);//总页数

        unset($page);
        unset($show);
        //分页
        $page = new Page($count,10);
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $show = $page->show();//底部页面选择展示

        /*$result= $m->field(array('ssg_account.account',
            'ssg_account.roleId','ssg_account.isValid','ssg_account.registerTime','ssg_account.id', 'ssg_admin_info.name','ssg_admin_info.sex'))
            ->join("ssg_member_info on ssg_account.id=ssg_admin_info.account_id")
            ->limit($page->firstRow,$page->listRows)
            ->select();*/
        $resultsql = "select DISTINCT b.account,a.name,a.sex ,b.roleId ,b.isValid,b.registerTime,b.id from ssg_account as b RIGHT JOIN ssg_admin_info as a on a.account_id = b.id 
      limit ".$page->firstRow.",".$page->listRows;

        $result = M()->query($resultsql);
//        dump($result);
         //搜索
        if(!empty($_POST['search'])){
            $searchsql = "select DISTINCT b.account,a.name,a.sex ,b.roleId ,b.isValid,b.registerTime from ssg_account as b
inner JOIN ssg_admin_info a on a.account_id = b.id && b.account LIKE '%".$_POST['search']."%' || a.name like '%".$_POST['search']."%'";
            $result = M()->query($searchsql);
            $_POST['search']=null;
        }


        //添加管理员
        if(isset($_POST['ad_add'])){
            $verify = "SELECT * FROM ssg_account where account=".$_POST['add_user'];
            if(empty(M()->query($verify))){
                $add = array();
                $add['user']=$_POST['add_user'];
                $add['password']=md5($_POST['add_password']);
                $add['verify']=$_POST['add_verify'];
                $add['name']=$_POST['add_name'];
                $add['sex']=$_POST['add_sex'];
                $add['age']=$_POST['add_age'];
                $add['email']=$_POST['add_email'];
                $add['phone']=$_POST['add_phone'];
                $add['province']=$_POST['add_province'];
                $add['city']=$_POST['add_city'];
                $add['area'] = $_POST['add_area'];
                $add['address']=$_POST['add_address'];
                $add['remark']=$_POST['add_remark'];
                $this->ad_add($add);
            }else{
                echo "<script>alert('添加失败！用户名已存在！')</script>";
            }
        }


        /**
         * 根据assign前面一个参数名称传值
         * result   默认点击查询结果
         * sum  总数结果行数
         */
        $this->assign('show',$show);
        $this->assign('sum',$sum);
        $this->assign('count',$count);
        $this->assign('result',$result);
        $this->display();
    }

    /** 添加管理员*/
    function ad_add($add=array()){

        $now = date('Y-m-d H:i:s');
        $sql1 = "insert into ssg_account(account,password,roleId,registerTime,system) VALUES('".$add['user']."','".$add['password']."',2,'".$now."','web')";
        $sql2 = "insert into ssg_admin_info(account_id,name,sex,age,email,linkPhone,pCode,cCode,aCode,linkAddres,remark) 
                 VALUES((select id from ssg_account where account = '".$add['user']."'),'".$add['name']."',".$add['sex'].",".$add['age'].",'".$add['email']."','".$add['phone']."','".$add['province']."','".$add['city']."','".$add['area']."','".$add['address']."','".$add['remark']."')";//相信自己老铁，没毛病

        //晚点用事务完成
        M()->execute($sql1);
        M()->execute($sql2);
        $this->redirect('Home/UserInfo/admin_Info',array(),0);//重定向到控制器，防止表单重复提交
    }

    /** 管理员删除*/
    function ad_delete(){
        $desql = "delete t1,t2 from ssg_account as t1,ssg_admin_info as t2 where t1.id=t2.account_id and t2.account_id=".$_POST['row'];
       if(M()->execute($desql)){
           echo "ok";
       }
    }

    /** 管理员重置*/
    function ad_reset(){
        $pwd = md5('ssg123456');
        $resql = "update ssg_account set password = ".$pwd." where id =".$_POST['id'];
        if(M()->execute($resql)){
//            $this->redirect('Home/UserInfo/admin_Info',array(),1,"<script>alert('密码重置完成！')</script>");
            echo "ok";
        }

    }

    /** 管理员编辑*/
    function ad_edit(){
        if(isset($_POST['re_add'])){
            $add = array();
            $add['id']=$_POST['re_id'];
            $add['name']=$_POST['re_name'];
            $add['sex']=$_POST['re_sex'];
            $add['age']=$_POST['re_age'];
            $add['email']=$_POST['re_email'];
            $add['phone']=$_POST['re_phone'];
            $add['province']=$_POST['re_province'];
            $add['city']=$_POST['re_city'];
            $add['area'] = $_POST['re_area'];
            $add['address']=$_POST['re_address'];
            $add['isValid']=$_POST['re_isValid'];

            $upsql = "update ssg_account a,ssg_admin_info d
set d.name='".$add['name']."',d.sex=".$add['sex'].",d.age=".$add['age'].",d.email='".$add['email']."',d.linkPhone='".$add['phone']."',d.pCode='".$add['province']."',d.cCode='".$add['city']."',d.aCode='".$add['area']."',
d.linkAddres='".$add['address']."',d.remark='".$add['remark']."',a.isValid=".$add['isValid']."
where d.account_id=".$add['id']." AND a.id=".$add['id'];
            if(M()->query($upsql)>0){
//                echo "<script>alert('修改完成！')</script>";
                $this->redirect('Home/UserInfo/admin_Info',array(),0.1,"<script>alert('修改完成！')</script>");
            }
        }
        $id = $_POST['id'];
        $editsql = "select DISTINCT b.account,a.name,a.sex ,b.roleId ,b.isValid,b.registerTime,b.id as re_id,a.email,a.linkPhone,a.pCode,a.cCode,a.aCode,a.linkAddres,a.remark,b.system,a.age from ssg_account as b
RIGHT JOIN ssg_admin_info as a on a.account_id = b.id where b.id=".$id;
        $result = M()->query($editsql);
        foreach ($result as $k => $v) {
           $this->ajaxReturn($v);
        }
    }





























    /**
     * 终端用户信息
     */
    function User_info(){

        //默认查询
        $m = M("ssg_account");
        $count = $m->count();//总条数
        $sum = ceil($count/10);//总页数


        //分页
        $page = new Page($count,10);
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $show = $page->show();//底部页面选择展示


        $result= $m->field(array('ssg_account.id',
            'ssg_account.account','ssg_member_info.name','ssg_account.isValid','ssg_account.system', 'ssg_account.registerTime','ssg_account.roleId'))
           ->join("ssg_member_info on ssg_account.id=ssg_member_info.account_id")
            ->limit($page->firstRow,$page->listRows)
            ->select();
//        dump($page);

//        dump($result);
        //搜索
        if(!empty($_POST['search'])) {


            unset($sum);
            unset($count);
            $s = M("ssg_account");
            $count = $s->count();//总条数
            $sum = ceil($count / 10);//总页数

            //分页
            unset($page);
            $page = new Page($count,10);
            $page->setConfig('first','首页');
            $page->setConfig('prev','上一页');
            $page->setConfig('next','下一页');
            $page->setConfig('last','尾页');

            $show = $page->show();//底部页面选择展示


            $result = $s->table("ssg_account as a")
//                ->filed(array('a.id', 'a.account', 'm.name', 'a.isValid', 'a.system', 'a.registerTime', 'a.roleId'))
                ->join("ssg_member_info as m on a.id=m.account_id")
                ->where("a.roleId=3 || a.roleId=4 and a.account like '%".$_POST['search']."' || m.name like '%".$_POST['search']."'")
                ->limit($page->firstRow, $page->listRows)
                ->select();
//            dump($result);
            /*dump($sum);
            dump($show);*/


        }

            /*        $searchsql = "SELECT a.id,a.account,m.name,a.isValid,a.system,a.registerTime,a.roleId from ssg_account as a
        INNER JOIN ssg_member_info as m on a.id=m.account_id
        where a.roleId=3 || a.roleId=4
        and a.account like '%".$_POST['search']."%' || m.name like '%".$_POST['search']."%' limit".$page->firstRow.",".$page->listRows;
            $result = M()->query($searchsql);*/




        //终端用户添加
        if(isset($_POST['user_add'])){
            $add['user']=$_POST['add_user'];
            $add['password']=md5($_POST['add_password']);
            $add['inviteCode'] = $_POST['inviteCode'];
            $sql1 = "insert into ssg_account(account,password,roleId,registerTime,system,isValid,isDelete) VALUES('".$add['user']."','".$add['password']."',3,'2017-03-12','web',1,0)";
            $sql2 = "insert into ssg_member_info(account_id,sex,age,inviteCode) VALUES((select id from ssg_account where account='".$add['user']."'),0,0,'".$add['inviteCode']."')";
            M()->execute($sql1);
            M()->execute($sql2);
            $this->redirect('Home/UserInfo/User_Info',array(),0.1,"<script>alert('添加成功！')</script>");//重定向到控制器，防止表单重复提交
        }

        $this->assign('show',$show);
        $this->assign('sum',$sum);
        $this->assign('count',$count);
        $this->assign('result',$result);
        $this->display();
    }
    /** 终端用户删除*/
    function user_delete(){
        $desql = "delete a,m from ssg_account as a INNER JOIN ssg_member_info m 
on a.id=m.account_id
 where a.id=".$_POST['value'];
        if(M()->execute($desql)){
            echo "ok";
        }
    }
    /**终端用户编辑*/
    function user_edit(){

        if(isset($_POST['user_edit'])){
            $add = array();

            $add['id']=$_POST['re_id'];
            $add['name']=$_POST['re_name'];
            $add['sex']=$_POST['re_sex'];
            $add['age']=$_POST['re_age'];
            $add['email']=$_POST['re_email'];
            $add['qq']=$_POST['re_qq'];
            $add['province']=$_POST['re_province'];
            $add['city']=$_POST['re_city'];
            $add['area'] = $_POST['re_area'];
            $add['address']=$_POST['re_address'];
            $add['isValid']=$_POST['re_isValid'];


            $edsql = "update ssg_account a,ssg_member_info d
set d.name='".$add['name']."',d.sex=".$add['sex'].",d.age=".$add['age'].",d.email='".$add['email']."',d.qq='".$add['qq']."',d.pCode='".$add['province']."',d.cCode='".$add['city']."',d.aCode='".$add['area']."',
d.address='".$add['address']."',a.isValid=".$add['isValid']."
where d.account_id=".$add['id']." AND a.id=".$add['id'];
            if(M()->execute($edsql)){
//                echo "<script>alert('修改完成！')</script>";
                $this->redirect('Home/UserInfo/User_Info',array(),0.1,"<script>alert('修改完成！')</script>");
            }
        }
        $id = $_POST['row'];
        $editsql = "select a.id as re_id,a.account,a.system,a.registerTime,a.roleId,a.isValid,m.* from ssg_account as a
INNER JOIN ssg_member_info as m
on a.id=m.account_id
where a.id=".$id;
        $result = M()->query($editsql);
        foreach ($result as $k => $v) {
            $this->ajaxReturn($v);
        }
    }

}